<?php
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
namespace app\controller\api\v1\activity;


use app\model\order\StoreOrder;
use app\model\user\User;
use app\model\user\UserBill;
use app\Request;
use app\services\activity\integral\StoreIntegralCategoryServices;
use app\services\activity\integral\StoreIntegralServices;
use app\services\store\SystemStoreServices;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;


/**
 * 积分商城
 * Class StoreIntegral
 * @package app\controller\api\activity
 */
class StoreIntegral
{

    protected $services;

    public function __construct(StoreIntegralServices $services)
    {
        $this->services = $services;
    }

	/**
	 * 积分商城首页数据
	 * @param SystemStoreServices $storeServices
	 * @param Request $request
	 * @return \think\Response
	 * @throws DataNotFoundException
	 * @throws DbException
	 * @throws ModelNotFoundException
	 */
    public function index(SystemStoreServices $storeServices, Request $request)
    {
		$data['banner'] = sys_data('integral_shop_banner') ?? [];// 积分商城banner
		$where = ['is_show' => 1];
		$where['is_host'] = 1;
		$uid = $request->hasMacro('uid') ? (int)$request->uid() : 0;
		$entryRules = $storeServices->getStoreIdByEntryRules($uid);
		$shop_operation_type = $entryRules['shop_operation_type'] ?? 1;
		if ($shop_operation_type == 3) {//单店模式不返回
			$storeId = $entryRules['store_id'] ?? 0;
			if ($storeId) {
				$where['applicable_store_id'] = $storeId;
				$list = $this->services->getIntegralList($where);
			} else {
				$list = [];
			}
		} else {
			$list = $this->services->getIntegralList($where);
		}
		$data['list'] = get_thumb_water($list, 'mid');
		$userInfo = $request->hasMacro('user')  ? $request->user()->toArray() : [];
		$data['integral'] = $userInfo['integral'] ?? 0;
		return app('json')->successful($data);
    }

    /**
     * 我的积分
     * @param Request $request
     * @return \think\Response
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getIntegralCount(Request $request)
    {
        $uid = $request->uid();
        $data["sum"] = User::where("uid", $uid)->value('integral') ?? 0; //用户积分
        $gain_sum = UserBill::where('type', 'gain')->where("uid", $uid)->where("category", "integral")->sum('number') ?? 0.00;  //消费得积分
        $share_sum = UserBill::where('type', 'share_new')->where("uid", $uid)->where("category", "integral")->sum('number') ?? 0.00;  //分享得积分

        $data['gain_sum'] = floor($gain_sum);
        $data['share_sum'] = floor($share_sum);
        return app('json')->successful($data);
    }


    /**
     * 商品列表
     * @param Request $request
     * @return mixed
     */
    public function lst(SystemStoreServices $storeServices, Request $request)
    {
		$where = $request->getMore([
			['store_name', ''],
			['priceOrder', ''],
			['salesOrder', ''],
			['range', ''],
		]);
		$where['is_show'] = 1;
		$uid = $request->hasMacro('uid') ? (int)$request->uid() : 0;
		$entryRules = $storeServices->getStoreIdByEntryRules($uid);
		$shop_operation_type = $entryRules['shop_operation_type'] ?? 1;
		if ($shop_operation_type == 3) {//单店模式不返回
			$storeId = $entryRules['store_id'] ?? 0;
			if ($storeId) {
				$where['applicable_store_id'] = $storeId;
				$list = $this->services->getIntegralList($where);
			} else {
				$list = [];
			}
		} else {
			$list = $this->services->getIntegralList($where);
		}
        return app('json')->successful(get_thumb_water($list, 'mid'));
    }

    /**
     * 积分商品详情
     * @param Request $request
     * @param $id
     * @return mixed
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function detail(Request $request, $id)
    {
        $data = $this->services->integralDetail((int)$request->uid(), $id);
        $data['exchange_num'] = StoreOrder::getExchangeNumById((int)$id);
        return app('json')->successful($data);
    }


	/**
	 * @param StoreIntegralCategoryServices $services
	 * @return \think\Response
	 */
	public function category(StoreIntegralCategoryServices $services)
	{
		return app('json')->successful($services->getTreeList());
	}

}
